/**
 * 断点续传测试代码
 */
const cheerio = require('cheerio');
const request = require('request')

const { httpGet } = require('../http-util');
const { POOL_SIZE, TIMEOUT, MAVEN_REPO_URL, NEXUS_URL } = require('../config')

const breakpoint = 'https://repo1.maven.org/maven2/activemq/activemq-web/3.2.2'

const compareUrl = url => breakpoint.startsWith(url) || url.replace(/\/$/, '') >= breakpoint

/**
 * 获取待下载的文件列表
 * @param {*} url 
 */
async function getFiles(url) {
    let res = await httpGet(url)
    let $ = cheerio.load(res)

    let dirs = []
    let files = []
    $('pre a').each((i, elem) => {
        let $a = $(elem)
        if ($a.html().endsWith('.jar') || $a.html().endsWith('.pom')) {
            files.push(url + $a.html())
        }
        if ($a.attr('href') != '../' && $a.attr('href').endsWith('/')) {
            dirs.push(url + $a.attr('href'))
        }
    })

    console.log('files : ', files)

    if (dirs.length) {
        dirs = dirs.filter(compareUrl)

        console.log('>>>>' + dirs[0])
        // 遍历目录
        for (let i in dirs) {
            await getFiles(dirs[i])
        }
    }
    // console.log('dir : ', dirs)



    // 下载文件
    // for (let i in files) {
    // await downloadFile(files[i])
    // }
}

getFiles(MAVEN_REPO_URL)